<?php

namespace app\api\controller;

use app\common\controller\Api;
use Exception;
use think\Db;
/**
 * 手机短信接口
 */
class Noipfraud extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
    }

    public function noipfraud()
    {
        $noipfraud = Db::name('noipfraud')
            ->where('status', 'normal')
            ->order('createtime', 'asc')
            ->limit(5)
            ->select();
        $succ = array();
        foreach ($noipfraud as $item) {
            $url = "https://".$item['name']."/6/api/campaigns.php?a=list";
            $datas = $this->geturl($url);
            if($datas) {
                $datas = json_decode($datas, true);
                if (!empty($datas)) {
                    Db::name('noipfraud_info')
                        ->where('noipfraud_id', $item['id'])
                        ->delete();
                    foreach ($datas as $data) {
                        $new = array();
                        $new['noipfraud_id'] = $item['id'];
                        $new['noipfraud_name'] = $item['name'];
                        $new['name'] = $data['name'];
                        $new['fakeurl'] = $data['fakeurl'];
                        $new['url'] = $data['realurl'][0]['url'];
                        $new['rules'] = $data['rules'];
                        $new['info'] = $data['info'];
                        //采集链接
                        $new_url = $new['url'].'/9999.html';
                        $new_urls = $new_url;
                        $arrContextOptions=array(
                            "ssl"=>array(
                                "verify_peer"=>false,
                                "verify_peer_name"=>false,
                            ),
                        );
                        $new_url = str_replace(" ","%20",$new_url);
                        $info = array();
                        try{
                            $info = file_get_contents($new_url, false, stream_context_create($arrContextOptions));
                            $info = iconv("gbk", "utf-8//IGNORE", $info);
                        } catch(Exception $e)
                        {
                            $new['msg'] = '链接无法采集';
                        }
                        if (!empty($info)) {
                            $infos = explode('URL=',$info);
                            $infos2 = explode('/>',$infos[1]);
                            $infos2[0] = str_replace("'","",$infos2[0]);
                            $infos2[0] = str_replace('" ',"",$infos2[0]);
                            $new['textinfo'] = str_replace(array("\r\n", "\r", "\n", " "), "",$infos2[0]);
                            //最后修改时间
                            $new_urls = explode('//',$new_urls);
                            $new_urls = '/www/wwwroot/'.$new_urls[1];
                            $a=filemtime($new_urls);
                            $new['updatetimes'] = $a;
                        }
                        $succ[] = Db::name('noipfraud_info')->insert($new);
                    }
                }
            }
            $noi['createtime'] = time();
            Db::name('noipfraud')
                ->where('id', $item['id'])
                ->update($noi);
        }
        $this->success('操作成功',$succ);
    }

    function geturl($url){
        $headerArray = array("Content-Type:application/json;",'Authorization:Bearer eyJyb2xlIjoiYXBpIiwidXNlcm5hbWUiOiJzeSIsImV4cCI6MTY0NTAxNjgyMn0=.MWEyYjBjZGY1Zjc2ZDA3NDRhNDI0NzhiYmQwYzZiZjgxYzAzZDdkMGJiY2JkNTYyNTAyNTY3ZTlhYWVkYzYyYw==');
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    public function noipfraud_text()
    {
        $noipfraud = Db::name('noipfraud_info')
            ->where('status', 'normal')
            ->order('updatetime', 'asc')
            ->limit(5)
            ->select();
        $succ = array();
        foreach ($noipfraud as $item) {
            $noipfraud_text = Db::name('noipfraud_text')->where('url', $item['url'])->find();
            if (empty($noipfraud_text)) {
                $nois['url'] = $item['url'];
                $nois['new_url'] = $item['url'].'/9999.html';

                $arrContextOptions=array(
                    "ssl"=>array(
                        "verify_peer"=>false,
                        "verify_peer_name"=>false,
                    ),
                );
                $info = array();
                try
                {
                    $info = file_get_contents($nois['new_url'], false, stream_context_create($arrContextOptions));
                    $info = iconv("gbk", "utf-8//IGNORE", $info);
                } catch(Exception $e)
                {

                }
                if (!empty($info)) {
                    $infos = explode('URL=',$info);
                    $infos2 = explode('/>',$infos[1]);
                    $infos2[0] = str_replace("'","",$infos2[0]);
                    $infos2[0] = str_replace('" ',"",$infos2[0]);
                    $nois['info'] = str_replace(array("\r\n", "\r", "\n", " "), "",$infos2[0]);
                    $succ[] = Db::name('noipfraud_text')->where('url', $item['url'])->insert($nois);
                    $noi['textinfo'] = $nois['info'];
                }
            }else{
                $noi['textinfo'] = $noipfraud_text['info'];
            }
            $noi['updatetime'] = time();
            Db::name('noipfraud_info')
                ->where('id', $item['id'])
                ->update($noi);
        }
        $this->success('操作成功',$succ);
    }
}
